#!/usr/bin/env python
# -*- coding:utf-8 -*-
from poster.streaminghttp import register_openers
import json
import urllib2
import urllib


def do_get(url):
    register_openers()
    request = urllib2.Request(url)
    request.get_method = lambda: "GET"
    result = urllib2.urlopen(request).read()
    return result


def do_post_json(url, data):
    register_openers()
    body_value = json.JSONEncoder().encode(data)
    request = urllib2.Request(url, body_value)
    request.add_header("Content-Type", "application/json")
    request.get_method = lambda: "POST"
    result = urllib2.urlopen(request).read()
    return result


def do_post(url, data):
    register_openers()
    body_value = urllib.urlencode(data)
    request = urllib2.Request(url, body_value)
    request.add_header("Content-Type", "application/x-www-form-urlencoded")
    request.get_method = lambda: "POST"
    result = urllib2.urlopen(request).read()
    return result


if __name__ == "__main__":
    # import feedparser
    # import re
    # rss = feedparser.parse("https://www.torlock.com/movies/rss.xml")
    # entries = rss.entries
    # for entry in entries:
    #     print entry.title
    #     print entry.link
    #     p = re.findall(r"\s+Seeds: (\d+)\n\s+Leechers: (\d+)\n\s+Size: (.*GB)", entry.description,re.M)
    #     print p

    import re
    ss = do_get("https://www.torlock.com/torrent/7411465/murder-on-the-orient-express-1080p-dual-audio-%5Bhindi-dd-5-1-english-dd-5-1%5D-esub-%5Bmw%5D.html")
    print re.findall("<a href=\"(magnet.*?)\">",ss,re.M)
